package conson.tech.manage.facade.controller.coupons;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.common.util.ExcelUtils;
import conson.tech.manage.coupons.entity.dto.*;
import conson.tech.manage.coupons.entity.vo.CouponMessagePageVo;
import conson.tech.manage.coupons.entity.vo.FinanceDataListExportVO;
import conson.tech.manage.coupons.entity.vo.FinanceDataPageVo;
import conson.tech.manage.coupons.service.CouponActivityService;
import conson.tech.manage.coupons.service.CouponsMessageCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * InterfaceName: ActivityController
 * Description: 领券活动控制器
 * Author: XYF
 * Date: 2020/7/21 10:37
 * Version: 1.0
 */
@Slf4j
@RestController
@Api(value = "领券活动控制器", tags = "领券活动控制器")
@RequestMapping((SecurityConstants.MANAGERAPI + "/coupon-activity"))
public class CouponActivityController extends BaseController {

    @Autowired
    private CouponActivityService couponActivityService;

    @Autowired
    private CouponsMessageCenterService couponsMessageCenterService;


    @ApiOperation(value = "查询优惠券批次列表")
    @ApiImplicitParam(name = "dto", value = "查询优惠券批次列表", required = true, dataType = "CouponsCenterActivityDto")
    @PostMapping("/coupons/center/query-coupons-list/{pageSize}/{pageIndex}")
    public Response queryCouponsList(@ApiParam("每页条数") @PathVariable("pageSize") Integer pageSize,
                                     @ApiParam("页码") @PathVariable("pageIndex") Integer pageIndex,
                                     @ApiParam(value = "查询参数") @RequestBody CouponsCenterActivityDto dto) {

        return Response.success(this.couponActivityService.queryCouponsList(pageSize, pageIndex, dto));
    }

    @ApiOperation(value = "新增领券活动")
    @ApiImplicitParam(name = "dto", value = "通用请求", required = true, dataType = "ActivitySaveDto")
    @PostMapping("/add")
    public Response addCouponActivity(HttpServletRequest request, @ApiParam(value = "新增领券活动参数", required = true) @Valid @RequestBody ActivitySaveDto dto) {

        log.info("CouponActivityController【新增领券活动】--> inParam:【{}】", JSONUtil.toJsonStr(dto));
//        Long currentUserId = this.getCurrentUserId(request);
//        String currentUsername = this.getCurrentUsername(request);
        Long currentUserId = 123456789L;
        String currentUsername = "广电测试";
        return couponActivityService.addCouponActivity(currentUserId, currentUsername, dto);
    }


    @ApiOperation(value = "修改领券活动")
    @ApiImplicitParam(name = "dto", value = "通用请求", required = true, dataType = "ActivityEditDto")
    @PostMapping("/edit/{activityId}")
    public Response editCouponActivity1(HttpServletRequest request,
                                        @PathVariable("activityId") Long activityId,
                                        @Valid @RequestBody ActivityEditDto dto) {

        log.info("CouponActivityController【修改领券活动】--> inParam:【{}】", JSONUtil.toJsonStr(dto));
//        Long currentUserId = this.getCurrentUserId(request);
//        String currentUsername = this.getCurrentUsername(request);
        Long currentUserId = 123456789L;
        String currentUsername = "广电测试";
        return couponActivityService.editCouponActivity(currentUserId, currentUsername, activityId, dto);
    }


    @ApiOperation(value = "领券活动列表")
    @ApiImplicitParam(name = "dto", value = "通用请求", required = true, dataType = "ActivityPageDto")
    @PostMapping("/list/{pageSize}/{pageIndex}")
    public Response couponActivityList(@NotNull(message = "请输入页码") @ApiParam(value = "页码", required = true) @PathVariable Integer pageIndex,
                                       @NotNull(message = "请输入每页条数") @ApiParam(value = "每页条数", required = true) @PathVariable Integer pageSize,
                                       @ApiParam(value = "查询参数", required = true) @RequestBody(required = false) ActivityPageDto dto) {

        log.info("CouponActivityController【领券活动列表】--> inParam:【{}】", JSONUtil.toJsonStr(dto));

        return Response.success(this.couponActivityService.couponActivityList(pageIndex, pageSize, dto));
    }

    @ApiOperation(value = "查看领券活动详情")
    @ApiImplicitParam(name = "id", value = "通用请求", required = true, dataType = "Long")
    @GetMapping("/show/{id}")
    public Response getCouponActivity(@PathVariable("id") Long id) {

        log.info("CouponActivityController【查看领券活动详情】--> inParam:【{}】", JSONUtil.toJsonStr(id));

        return couponActivityService.getCouponActivity(id);
    }

    @ApiOperation(value = "查看优惠券消息中心详情")
    @ApiImplicitParam(name = "id", value = "活动或优惠券id", required = true, dataType = "Long")
    @GetMapping("/msg/{id}/{type}")
    public Response getCouponMsg(@NotNull(message = "活动id或优惠券id") @ApiParam(value = "活动id或优惠券id", required = true) @PathVariable("id") Long id,
                                 @NotNull(message = "消息类型(1 活动来源 2优惠券来源)") @ApiParam(value = "消息类型", required = true) @PathVariable("type") Integer type) {

        log.info("CouponActivityController【查看优惠券消息中心详情1】--> inParam:【{}】", JSONUtil.toJsonStr(id));

        return couponsMessageCenterService.getCouponMsg1(id, type);
    }

    @ApiOperation(value = "优惠券消息中心修改")
    @ApiImplicitParam(name = "dto", value = "修改消息参数", required = true, dataType = "CouponMsgEditDto")
    @PostMapping("/msg/edit/{id}/{type}")
    public Response updateCouponMsg(@NotNull(message = "活动id或优惠券id") @ApiParam(value = "活动id或优惠券id", required = true) @PathVariable("id") Long id,
                                    @NotNull(message = "消息类型(1 活动来源 2优惠券来源)") @ApiParam(value = "消息类型", required = true) @PathVariable("type") Integer type,
                                    @ApiParam(value = "修改消息参数", required = true) @Valid @RequestBody(required = false) CouponMsgEditDto dto) {

        log.info("CouponActivityController【优惠券消息中心修改1】--> inParam:【{}】", JSONUtil.toJsonStr(id));
        Long currentUserId = 123456789L;
        String currentUsername = "广电测试";
        return couponsMessageCenterService.updateCouponMsg1(currentUserId, currentUsername, id, type, dto);
    }

    @ApiOperation(value = "作废领券活动")
    @ApiImplicitParam(name = "id", value = "通用请求", required = true, dataType = "Long")
    @GetMapping("/invalid/{id}")
    public Response giveUpCouponActivity(HttpServletRequest request, @PathVariable("id") Long id) {

        log.info("CouponActivityController【作废领券活动】--> inParam:【{}】", JSONUtil.toJsonStr(id));
//        Long currentUserId = this.getCurrentUserId(request);
//        String currentUsername = this.getCurrentUsername(request);
        Long currentUserId = 123456789L;
        String currentUsername = "广电测试";
        return couponActivityService.giveUpCouponActivity(currentUserId, currentUsername, id);
    }

    @ApiOperation(value = "强制结束领券活动")
    @ApiImplicitParam(name = "id", value = "通用请求", required = true, dataType = "Long")
    @GetMapping("/over/{id}")
    public Response overCouponActivity(HttpServletRequest request, @PathVariable("id") Long id) {

        log.info("CouponActivityController【强制结束领券活动】--> inParam:【{}】", JSONUtil.toJsonStr(id));

//        Long currentUserId = this.getCurrentUserId(request);
//        String currentUsername = this.getCurrentUsername(request);
        Long currentUserId = 123456789L;
        String currentUsername = "广电测试";
        return couponActivityService.overCouponActivity(currentUserId, currentUsername, id);
    }

    @ApiOperation(value = "导入用户模板下载")
    @GetMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response, HttpServletRequest request) {
        String fileName = "order";
        Workbook workbook = new XSSFWorkbook();
        try {
            ExcelUtils.downLoadExcel(fileName, response, workbook, request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "查看发放明细列表")
    @ApiImplicitParam(name = "dto", value = "通用请求", required = true, dataType = "BindingDetailDto")
    @PostMapping("/binding-detail/{pageSize}/{pageIndex}")
    public Response bindingDetail(@NotNull(message = "请输入页码") @ApiParam(value = "页码", required = true) @PathVariable Integer pageIndex,
                                  @NotNull(message = "请输入每页条数") @ApiParam(value = "每页条数", required = true) @PathVariable Integer pageSize,
                                  @ApiParam(value = "查询参数", required = true) @RequestBody(required = false) BindingDetailDto dto) {


        log.info("CouponActivityController【查看发放明细列表】--> inParam:【{}】", JSONUtil.toJsonStr(dto));

        return Response.success(this.couponActivityService.bindingDetail(pageIndex, pageSize, dto));
    }

    @ApiOperation(value = "当前批次优惠券剩余数量")
    @ApiImplicitParam(name = "activityId", value = "活动id", required = true, dataType = "Long")
    @GetMapping("/remain/{activityId}")
    public Response activityCouponRemain(@NotNull(message = "活动id") @ApiParam(value = "活动id", required = true) @PathVariable("activityId") Long activityId) {


        log.info("CouponActivityController【当前批次优惠券剩余数量】--> inParam:【{}】", JSONUtil.toJsonStr(activityId));

        return Response.success(this.couponActivityService.activityCouponRemain(activityId));
    }

    @ApiOperation(value = "定向发放导入用户列表")
    @ApiImplicitParam(name = "id", value = "通用请求", required = true, dataType = "Long")
    @PostMapping("/binding/{id}")
    public Response bindingUsers(HttpServletRequest request, MultipartFile file,
                                 @ApiParam(value = "活动id", required = true) @PathVariable("id") Long id,
                                 HttpServletResponse response) throws Exception {

        log.info("CouponActivityController【定向发放导入用户列表】--> inParam:【{}】", JSONUtil.toJsonStr(file));

//        Long currentUserId = this.getCurrentUserId(request);
        Long currentUserId = 123456789L;
        String currentUsername = "广电测试";
        return couponActivityService.bindingUsers(currentUserId, file, id,response);
    }

    @ApiOperation(value = "优惠券消息中心列表")
    @ApiImplicitParam(name = "dto", value = "通用请求", required = true, dataType = "CouponMessagePageDto")
    @PostMapping("/msg/{pageSize}/{pageIndex}")
    public Response<Page<CouponMessagePageVo>> couponMessageList(@NotNull(message = "请输入页码") @ApiParam(value = "页码", required = true) @PathVariable Integer pageIndex,
                                                                 @NotNull(message = "请输入每页条数") @ApiParam(value = "每页条数", required = true) @PathVariable Integer pageSize,
                                                                 @ApiParam(value = "查询参数", required = true) @RequestBody(required = false) CouponMessagePageDto dto) {

        log.info("CouponActivityController【优惠券消息中心列表】--> inParam:【{}】", JSONUtil.toJsonStr(dto));

        return Response.success(this.couponActivityService.couponMessageList1(pageIndex, pageSize, dto));
    }


    @ApiOperation(value = "财务结算报表列表")
    @ApiImplicitParam(name = "dto", value = "通用请求", required = true, dataType = "FinanceDataPageDto")
    @PostMapping("/finance/{pageSize}/{pageIndex}")
    public Response<Page<FinanceDataPageVo>> financeDataList(@NotNull(message = "请输入页码") @ApiParam(value = "页码", required = true) @PathVariable Integer pageIndex,
                                    @NotNull(message = "请输入每页条数") @ApiParam(value = "每页条数", required = true) @PathVariable Integer pageSize,
                                    @ApiParam(value = "查询参数", required = true) @RequestBody(required = false) FinanceDataPageDto dto) {

        log.info("CouponActivityController【财务结算报表列表】--> inParam:【{}】", JSONUtil.toJsonStr(dto));

        return Response.success(this.couponActivityService.financeDataList(pageIndex, pageSize, dto));
    }

    @ApiOperation(value = "财务结算报表结算")
    @ApiImplicitParam(name = "id", value = "财务数据id", required = true, dataType = "Long")
    @GetMapping("/settle/{id}")
    public Response settleFinanceData(HttpServletRequest request, @PathVariable("id") Long id) {

        log.info("CouponActivityController【财务结算报表结算】--> inParam:【{}】", JSONUtil.toJsonStr(id));

//        Long currentUserId = this.getCurrentUserId(request);
//        String currentUsername = this.getCurrentUsername(request);
        Long currentUserId = 123456789L;
        String currentUsername = "广电测试";
        return couponActivityService.settleFinanceData(currentUserId, currentUsername, id);
    }

    @ApiOperation(value = "财务结算报表导出", notes = "财务结算报表导出")
    @ApiImplicitParam(name = "dto", value = "请求入参", required = true, dataType = "FinanceDataPageDto")
    @PostMapping("/export/{pageSize}/{pageIndex}")
    public void export(@NotNull(message = "请输入页码") @ApiParam(value = "页码", required = true) @PathVariable Integer pageIndex,
                       @NotNull(message = "请输入每页条数") @ApiParam(value = "每页条数", required = true) @PathVariable Integer pageSize,
                       @ApiParam(value = "查询参数", required = true) @RequestBody(required = false) FinanceDataPageDto dto,
                       HttpServletRequest request, HttpServletResponse response) {

        Response response1 = this.financeDataList(pageIndex, pageSize, dto);
        Page<FinanceDataPageVo> page = (Page<FinanceDataPageVo>) response1.getResult();
        String fileName = "finance";
        try {
            ExcelUtils.export(fileName, page.getRecords(), FinanceDataListExportVO.class, response);
        } catch (Exception e) {
            log.error("财务结算报表导出失败，exception", e);
        }
    }

    @ApiOperation(value = "通用日志列表")
    @ApiImplicitParam(name = "id", value = "活动id", required = true, dataType = "Long")
    @GetMapping("/log/{pageSize}/{pageIndex}/{id}/{type}")
    public Response couponActivityLogList(@NotNull(message = "请输入页码") @ApiParam(value = "页码", required = true) @PathVariable Integer pageIndex,
                                          @NotNull(message = "请输入每页条数") @ApiParam(value = "每页条数", required = true) @PathVariable Integer pageSize,
                                          @ApiParam(value = "日志操作id", required = true) @PathVariable Long id,
                                          @ApiParam(value = "日志类型(0 优惠券操作日志 1 活动操作日志，2财务操作日志)", required = true) @PathVariable Integer type) {

        log.info("CouponActivityController【通用日志列表】--> inParam:【{}】", JSONUtil.toJsonStr(id));

        return Response.success(this.couponActivityService.couponActivityLogList(pageIndex, pageSize, id, type));
    }

}
